9 touch commands introduced in Linux

The touch command is used to create an empty file. It can also change the existing file time stamp on Unix and Linux systems. Changing the timestamp here means updating the access and modification time of files and directories.

Let's take a look at the syntax and options of the touch command:

grammar:

# touch {option} {file}

Options used in the touch command:

9 touch commands introduced in Linux

touch-command-opTIons

In this article, we will introduce nine useful touch command examples in Linux.

Example: 1 Create an empty file with touch

To create an empty file using the touch command on a Linux system, type touch and enter the file name. As follows:

[root@linuxtechi ~]# touch devops.txt

[root@linuxtechi ~]# ls -l devops.txt

-rw-r--r--. 1 root root 0 Mar 29 22:39 devops.txt

Example: 2 Using touch to create a bulk empty file

There may be situations where we have to create a lot of empty files for some tests, which can be easily implemented using the touch command:

[root@linuxtechi ~]# touch sysadm-{1..20}.txt

In the above example, we created 20 empty files named sysadm-1.txt through sysadm-20.txt. You can change the names and numbers as needed.

Example: 3 Change/update access time of files and directories

Suppose we want to change the access time for the file named devops.txt, use the -a option in the touch command, and enter the file name. As follows:

[root@linuxtechi ~]# touch -a devops.txt

Now use the stat command to verify that the file's access time has been updated:

[root@linuxtechi ~]# stat devops.txt

File: 'devops.txt'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 67324178 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2018-03-29 23:03:10.902000000 -0400

Modify: 2018-03-29 22:39:29.365000000 -0400

Change: 2018-03-29 23:03:10.902000000 -0400

Birth:

-

Change the access time of the directory:

Assuming we have an nfsshare folder in the /mnt directory, let's change the access time for this folder with the following command:

[root@linuxtechi ~]# touch -m /mnt/nfsshare/

[root@linuxtechi ~]# stat /mnt/nfsshare/

File: '/mnt/nfsshare/'

Size: 6 Blocks: 0 IO Block: 4096 directory

Device: fd00h/64768d Inode: 2258 Links: 2

Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:mnt_t:s0

Access: 2018-03-29 23:34:38.095000000 -0400

Modify: 2018-03-03 10:42:45.194000000 -0500

Change: 2018-03-29 23:34:38.095000000 -0400

Birth: -

Example: 4 Change the access time without creating a new file

In some cases, if the file exists, we want to change the access time of the file and avoid creating the file. Use the -c option in the touch command. If the file exists, then we can change the access time of the file. If it doesn't exist, we won't create it.

[root@linuxtechi ~]# touch -c sysadm-20.txt

[root@linuxtechi ~]# touch -c winadm-20.txt

[root@linuxtechi ~]# ls -l winadm-20.txt

Ls: cannot access winadm-20.txt: No such file or directory

Example: 5 Change the modification time of files and directories

Using the -m option in the touch command, we can change the modification time of files and directories.

Let's change the change time of the file named devops.txt:

[root@linuxtechi ~]# touch -m devops.txt

Now use the stat command to verify that the modification time has changed:

[root@linuxtechi ~]# stat devops.txt

File: 'devops.txt'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 67324178 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2018-03-29 23:03:10.902000000 -0400

Modify: 2018-03-29 23:59:49.106000000 -0400

Change: 2018-03-29 23:59:49.106000000 -0400

Birth: -

Similarly, we can change the modification time of a directory:

[root@linuxtechi ~]# touch -m /mnt/nfsshare/

Cross-validation of access and modification times using stat:

[root@linuxtechi ~]# stat devops.txt

File: 'devops.txt'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 67324178 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2018-03-30 00:06:20.145000000 -0400

Modify: 2018-03-30 00:06:20.145000000 -0400

Change: 2018-03-30 00:06:20.145000000 -0400

Birth: -

Example: 7 Set access and modification time to a specific date and time

Whenever we use the touch command to change the access and modification time of files and directories, it sets the current time to the access and modification time of that file or directory.

Suppose we want to set a specific date and time as the file's access and modification times. This can be done using the -c and -t options in the touch command.

The date and time can be specified using the following format:

{CCYY}MMDDhhmm.ss

among them:

CC - first two digits of the year

YY - last two digits of the year

MM - Month (01-12)

DD - Day (01-31)

Hh - hour (00-23)

Mm - minutes (00-59)

Let us set the access and modification time of the devops.txt file to the next time (October 19, 2025, 18:20).

[root@linuxtechi ~]# touch -c -t 202510191820 devops.txt

Use the stat command to view update access and modification times:

9 touch commands introduced in Linux

Stat-command-output-linux

Set the access and modification time based on the date string, use the -d option in the touch command, and specify the date string followed by the file name. As follows:

[root@linuxtechi ~]# touch -c -d "2010-02-07 20:15:12.000000000 +0530" sysadm-29.txt

Use the stat command to verify the status of the file:

[root@linuxtechi ~]# stat sysadm-20.txt

File: 'sysadm-20.txt'

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 67324189 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2010-02-07 20:15:12.000000000 +0530

Modify: 2010-02-07 20:15:12.000000000 +0530

Change: 2018-03-30 10:23:31.584000000 +0530

Birth: -

Note: In the above command, if we do not specify -c, the touch command will create a new file if the file does not exist on the system, and set the time stamp as given in the command.

Example: 8 Setting a Timestamp Using Reference Files (-r)

In the touch command, we can use a reference file to set the timestamp of a file or directory. Suppose that I want to set the same time stamp on the devops.txt file as the sysadm-20.txt file, which can be easily implemented using the -r option in the touch command.

grammar:

# touch -r {reference file} true file

[root@linuxtechi ~]# touch -r sysadm-20.txt devops.txt

Example: 9 Changing access and modification times on symbolic link files

By default, whenever we try to use the touch command to change the timestamp of a symbolic link file, it will only change the timestamp of the original file. If you want to change the time stamp of the symbolic link file, you can use the -h option in the touch command.

grammar:

# touch -h {symlink file}

[root@linuxtechi opt]# ls -l /root/linuxgeeks.txt

Lrwxrwxrwx. 1 root root 15 Mar 30 10:56 /root/linuxgeeks.txt -》 linuxadmins.txt

[root@linuxtechi ~]# touch -t 203010191820 -h linuxgeeks.txt

[root@linuxtechi ~]# ls -l linuxgeeks.txt

Lrwxrwxrwx. 1 root root 15 Oct 19 2030 linuxgeeks.txt ->> linuxadmins.txt

This is the whole story of this tutorial. I hope these examples will help you understand the touch command. Please share your valuable opinions and comments.

19 Inch PC Monitor

Guangzhou Bolei Electronic Technology Co., Ltd. , https://www.nzpal.com